/**
 * 资源加载演示程序
 * author   : jxyi
 * date     : 2017-3-12
 */
cc.Class({
    extends: cc.Component,

    properties: {

    },

    // use this for initialization
    onLoad: function () {
        this.hero = null;
        this.boom = null;

        cc.find('actBtn', this.node).on(cc.Node.EventType.TOUCH_END
            , this.onBtnClick, this);

        // 参考 http://www.cocos.com/docs/creator/scripting/load-assets.html
        cc.loader.loadRes('persist/prefab/plane_hero', function(err, prefab){
            var hero = cc.instantiate(prefab);
            hero.setPosition(0, 0);
            this.node.addChild(hero);

            this.hero = hero;
        }.bind(this)); // TODO：看看 bind 有什么用

        cc.loader.loadRes('persist/prefab/effect_hit', function(err, prefab){
            var boom = cc.instantiate(prefab);
            boom.setPosition(cc.Vec2.ZERO);
            this.node.addChild(boom);
            boom.opacity = 0;

            boom.getComponent(cc.Animation).on('play', function(event){
                boom.opacity = 255;
            }.bind(this), this);

            boom.getComponent(cc.Animation).on('stop', function(event){
                boom.opacity = 0;
            }.bind(this), this);

            this.boom = boom;
        }.bind(this));
    },

    onBtnClick: function(event){
        if (this.boom){
            var animation = this.boom.getComponent(cc.Animation);
            animation.play();
        }
    }
});
